package com.njwb.day15.zqzb.ios.io4;

import java.io.*;

public class BufferedOutAndInputDemo {
    public static void main(String[] args) {
        //既有读取，又有写入 (节点流)
        InputStream inputStream = null ;
        OutputStream outputStream = null ;
        BufferedInputStream bis = null ;
        BufferedOutputStream bos = null;

        try {
            inputStream   = new FileInputStream("C:\\Users\\Administrator\\Desktop\\VSCodeUserSetup-x64-1.100.2.exe");
            outputStream = new FileOutputStream("F:\\vscode.exe");
            bis = new BufferedInputStream(inputStream);
            bos = new BufferedOutputStream(outputStream);
            //读写操作
            byte[] buffer = new byte[1024] ;
            int length = 0 ;
            //读
            long startTime = System.currentTimeMillis() ;
            while((length = bis.read(buffer))!=-1) {
                //写
                bos.write(buffer,0,length);
            }
            long endTime = System.currentTimeMillis();
            System.out.println("拷贝用了："+(endTime-startTime)+"毫秒");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bos!=null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bis!=null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(inputStream!=null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(outputStream!=null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
